package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.service.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/seckillGood")
public class SeckillGoodController {
    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RedisService redisService;

    @RequestMapping("query")
    public Result query() {
 //从数据库获取vo集合
       // List<SeckillGoodVO> vos = seckillGoodService.query();
        List<SeckillGoodVO> vos = seckillGoodService.queryByRedis();
        return Result.success(vos);
    }

    @RequestMapping("queryById")
    public Result queryById(Long seckillId) {
          //从数据库获取vo
       //SeckillGoodVO VO = seckillGoodService.queryById(seckillId);
        //从redis里面获取vo
        SeckillGoodVO VO = seckillGoodService.queryByIdByRedis(seckillId);
        return Result.success(VO);
    }

    @RequestMapping("initDate")
    public String initDate() {

        List<SeckillGoodVO> vos = seckillGoodService.query();
        for (SeckillGoodVO vo : vos) {
            //把库存放到redis里(预库存)
            redisService.set(SeckillKeyPrefix.SECKILL_STOCK_COUNT, vo.getId() + "", vo.getStockCount());
            //把vo集合放到redis里
            redisService.hSet(SeckillKeyPrefix.REDIS_SECKILL_VO, "", vo.getId() + "", vo);
        }

        return "初始化成功";
    }

}
